/*
Copyright (c) 2008,2009, David Beck

Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions
are met:

1. Redistributions of source code must retain the above copyright
   notice, this list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright
   notice, this list of conditions and the following disclaimer in the
   documentation and/or other materials provided with the distribution.

THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/

#ifndef _rt0d4_rx_conn_priv_hh_included_
#define _rt0d4_rx_conn_priv_hh_included_

/**
   @file conn_priv.hh
   @brief conn_priv contains the connection related data

   This is an internal class that is based on rt0d4::sec classes
*/

#include "common.h"
#ifdef __cplusplus
#include <memory>

namespace rt0d4
{
  namespace rx
  {
    class secobj_priv;

    /** @brief Internal class that stores Rx connection related data */
    class conn_priv
    {
    public:
      
      /* private data */
      void set_conn_data(void * dta);
      void * get_conn_data();

      /* authenticated ? */
      void set_authenticated(bool yesno);
      bool get_authenticated() const;

      /* challenge */
      bool create_challenge();
      const unsigned char * get_challenge(size_t & len);
      bool set_challenge(unsigned char * data, size_t len);

      /* response */
      const unsigned char * get_response(size_t & len);
      bool set_response(unsigned char * data, size_t len);

      /* session key */
      const unsigned char * get_session_key();

      /* peer cert */
      const unsigned char * get_peer_cert(size_t & len);

      /* login, pass */
      const char * get_peer_login();
      const char * get_peer_password();

      /* allowed constructors and destructor */
      conn_priv(const secobj_priv & sepr);
      ~conn_priv();

      /* counters */
      inline unsigned long n_prepare_packet_ok(bool inc=false) {
        return ( inc==true ? ++n_prepare_packet_ok_ : n_prepare_packet_ok_ ); }

      inline unsigned long n_send_packet_ok(bool inc=false) {
        return ( inc==true ? ++n_send_packet_ok_ : n_send_packet_ok_ ); }

      inline unsigned long n_check_auth_ok(bool inc=false) {
        return ( inc==true ? ++n_check_auth_ok_ : n_check_auth_ok_ ); }

      inline unsigned long n_create_challenge_ok(bool inc=false) {
        return ( inc==true ? ++n_create_challenge_ok_ : n_create_challenge_ok_ ); }

      inline unsigned long n_get_challenge_ok(bool inc=false) {
        return ( inc==true ? ++n_get_challenge_ok_ : n_get_challenge_ok_ ); }

      inline unsigned long n_get_response_ok(bool inc=false) {
        return ( inc==true ? ++n_get_response_ok_ : n_get_response_ok_ ); }

      inline unsigned long n_check_response_ok(bool inc=false) {
        return ( inc==true ? ++n_check_response_ok_ : n_check_response_ok_ ); }

      inline unsigned long n_check_packet_ok(bool inc=false) {
        return ( inc==true ? ++n_check_packet_ok_ : n_check_packet_ok_ ); }
      /* */
      inline unsigned long n_prepare_packet_err(bool inc=false) {
        return ( inc==true ? ++n_prepare_packet_err_ : n_prepare_packet_err_ ); }

      inline unsigned long n_send_packet_err(bool inc=false) {
        return ( inc==true ? ++n_send_packet_err_ : n_send_packet_err_ ); }

      inline unsigned long n_check_auth_err(bool inc=false) {
        return ( inc==true ? ++n_check_auth_err_ : n_check_auth_err_ ); }

      inline unsigned long n_create_challenge_err(bool inc=false) {
        return ( inc==true ? ++n_create_challenge_err_ : n_create_challenge_err_ ); }

      inline unsigned long n_get_challenge_err(bool inc=false) {
        return ( inc==true ? ++n_get_challenge_err_ : n_get_challenge_err_ ); }

      inline unsigned long n_get_response_err(bool inc=false) {
        return ( inc==true ? ++n_get_response_err_ : n_get_response_err_ ); }

      inline unsigned long n_check_response_err(bool inc=false) {
        return ( inc==true ? ++n_check_response_err_ : n_check_response_err_ ); }

      inline unsigned long n_check_packet_err(bool inc=false) {
        return ( inc==true ? ++n_check_packet_err_ : n_check_packet_err_ ); }

      struct impl;

    private:
      /* need a secobj_priv to be created */
      conn_priv();

      /* private data */
      std::auto_ptr<impl> impl_;
      /* copying not allowed */
      conn_priv(const conn_priv & other);
      conn_priv & operator=(const conn_priv & other);

    private:
      /* counter access is not hidded in impl for performance reasons */
      unsigned long n_prepare_packet_ok_;
      unsigned long n_send_packet_ok_;
      unsigned long n_check_auth_ok_;
      unsigned long n_create_challenge_ok_;
      unsigned long n_get_challenge_ok_;
      unsigned long n_get_response_ok_;
      unsigned long n_check_response_ok_;
      unsigned long n_check_packet_ok_;
      /* */
      unsigned long n_prepare_packet_err_;
      unsigned long n_send_packet_err_;
      unsigned long n_check_auth_err_;
      unsigned long n_create_challenge_err_;
      unsigned long n_get_challenge_err_;
      unsigned long n_get_response_err_;
      unsigned long n_check_response_err_;
      unsigned long n_check_packet_err_;
    };
  }
}

#endif /* __cplusplus */
#endif /* _rt0d4_rx_conn_priv_hh_included_ */
